﻿using System.Threading;
using System;
using AChance.Func;
using AChance.Bll.Func;
using AChance.Bll.Trans;

namespace FutureSerWeb.Func
{
    public static class AppTask
    {
        /// <summary>
        /// <para>系统线程</para>
        /// </summary>
        public static void System()
        {
            DateTime calendar;
            const int clearLogsSpaceDays = 1;
            DateTime clearLogsLastTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 0, 0);
            const int clearTempSpaceHours = 1;
            DateTime clearTempLastTime = DateTime.Now;
            const int clearFilesSpaceDays = 1;
            DateTime clearFilesLastTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 1, 0, 0);
            const int updateCacheSpaceSeconds = 30;
            DateTime updateCacheLastTime = DateTime.Now;
            const int updateRedisSpaceMinutes = 5;
            DateTime updateRedisLastTime = DateTime.Now;

            while (!FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
            {
                #region 清理日志目录中的过期文件
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                try
                {
                    calendar = clearLogsLastTime.AddDays(clearLogsSpaceDays);
                    if (calendar < DateTime.Now)
                    {
                        clearLogsLastTime = calendar;
                        FunLogs.ClearExpired();
                    }
                }
                catch (Exception ex)
                {
                    FunLogs.LogError(ex);
                }
                #endregion

                #region 清理临时目录中的过期文件
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                try
                {
                    calendar = clearTempLastTime.AddHours(clearTempSpaceHours);
                    if (calendar < DateTime.Now)
                    {
                        clearTempLastTime = calendar;
                        FunTemp.ClearExpired();
                    }
                }
                catch (Exception ex)
                {
                    FunLogs.LogError(ex);
                }
                #endregion

                #region 清理文件目录中的过期文件
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                try
                {
                    calendar = clearFilesLastTime.AddDays(clearFilesSpaceDays);
                    if (calendar < DateTime.Now)
                    {
                        clearFilesLastTime = calendar;
                        FunFiles.ClearExpired();
                    }
                }
                catch (Exception ex)
                {
                    FunLogs.LogError(ex);
                }
                #endregion

                #region 更新缓存数据
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                try
                {
                    calendar = updateCacheLastTime.AddSeconds(updateCacheSpaceSeconds);
                    if (calendar < DateTime.Now)
                    {
                        updateCacheLastTime = calendar;
                        BllTrans.Invoke<TrnSql.ChgCache>();
                    }
                }
                catch (Exception ex)
                {
                    FunLogs.LogError(ex);
                }
                #endregion

                #region 更新Redis用户期限
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                try
                {
                    calendar = updateRedisLastTime.AddMinutes(updateRedisSpaceMinutes);
                    if (calendar < DateTime.Now)
                    {
                        updateRedisLastTime = calendar;
                        BllTrans.Invoke<TrnRedis.ChgUserExpiry>();
                    }
                }
                catch (Exception ex)
                {
                    FunLogs.LogError(ex);
                }
                #endregion

                #region 线程挂起
                if (FunTask.Get(FunTask.Name.SYSTEM).IsWantStop)
                    break;
                Thread.Sleep(2000);
                #endregion
            }
        }
        /// <summary>
        /// <para>业务线程</para>
        /// </summary>
        public static void Business()
        {
        }


    }
}